package com.example.gulimall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.gulimall.product.entity.SpuInfoEntity;
import com.example.gulimall.product.vo.SpuSaveVo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.gulimall.common.utils.PageUtils;
import com.example.gulimall.common.utils.Query;

import com.example.gulimall.product.dao.SkuInfoDao;
import com.example.gulimall.product.entity.SkuInfoEntity;
import com.example.gulimall.product.service.SkuInfoService;
import org.springframework.transaction.annotation.Transactional;


/**
 * @author xiehaidong
 */
@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        QueryWrapper<SkuInfoEntity> skuInfoEntityQueryWrapper = new QueryWrapper<>();
        String catelogId = (String) params.get("catelogId");
        if (!com.mysql.cj.util.StringUtils.isNullOrEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId)) {
            skuInfoEntityQueryWrapper.lambda().eq(SkuInfoEntity::getCatalogId, catelogId);
        }
        String brandId = (String) params.get("brandId");
        if (!com.mysql.cj.util.StringUtils.isNullOrEmpty(brandId) && !"0".equalsIgnoreCase(brandId)) {
            skuInfoEntityQueryWrapper.lambda().eq(SkuInfoEntity::getBrandId, brandId);
        }
        String min = (String) params.get("min");
        if (min != null) {
            skuInfoEntityQueryWrapper.lambda().ge(SkuInfoEntity::getPrice, min);
        }
        String max = (String) params.get("max");
        if (StringUtils.isNullOrEmpty(max)) {
            try {
                BigDecimal maxPrice = new BigDecimal(max);
                if (maxPrice.compareTo(BigDecimal.ZERO) == 1) {
                    skuInfoEntityQueryWrapper.lambda().le(SkuInfoEntity::getPrice, maxPrice);
                }
            } catch (Exception e) {
            }
        }
        String key = (String) params.get("key");
        if (!com.mysql.cj.util.StringUtils.isNullOrEmpty(key)){
            skuInfoEntityQueryWrapper.and((w->{
                w.lambda().eq(SkuInfoEntity::getSkuId,key).or().like(SkuInfoEntity::getSkuName,key);
            }));
        }
        IPage<SkuInfoEntity> page= this.page(new Query<SkuInfoEntity>().getPage(params),skuInfoEntityQueryWrapper);

        return new PageUtils(page);
    }



}
